package y2017.m10.d12;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.UUID;

public class TcpServer {
    public static void main(String[] args) throws Exception {
        ServerSocket ss = new ServerSocket(10010);
        while (true) {
            Socket sk = ss.accept();
            new Thread(new ServerThread(sk)).start();
        }

    }
}

class UUIDUtils {
    static String getFileName() {
        String fileName = UUID.randomUUID().toString();
        fileName = fileName.replaceAll("-", "") + ".txt";
        return fileName;
    }
}

class ServerThread implements Runnable {
    private Socket sk;

    ServerThread(Socket sk) {
        this.sk = sk;
    }

    @Override
    public void run() {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(sk.getInputStream()));
            BufferedWriter bw = new BufferedWriter(new FileWriter(UUIDUtils.getFileName()));

            String line;
            while ((line = br.readLine()) != null) {
                bw.write(line);
                bw.newLine();
                bw.flush();
//                System.out.println(line);
            }
            bw.close();
            br.close();

        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
    }

}